﻿unit FileInfo;

interface

uses
 SysUtils,Classes,Windows, Messages, Variants, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, ToolWin, R4FuncKeys, Grids, R4Grid, Buttons,EMMemList;

type
  TFileInfo = record
    FileName : string;
    FilePath : string;
    FileType : string;
    FileSize : Double;
    FileDate : string;
  end;
  PFileInfo = ^TFileInfo;

procedure InitFileInfo(var FileInfo : TFileInfo;FileName,FilePath,FileType : string; FileSize : Double; FileDate : string);

function SortByName(FileInfo1,FileInfo2 : Pointer):Integer;

function SortByPath(FileInfo1,FileInfo2 : Pointer):Integer;

function SortByType(FileInfo1,FileInfo2 : Pointer):Integer;

function SortBySize(FileInfo1,FileInfo2 : Pointer):Integer;

function SortByDate(FileInfo1,FileInfo2 : Pointer):Integer;

procedure Sorts(var List : TEMMemList;Num:Integer;IsSorted:Integer);

implementation

var
  Sorted : Integer;

procedure InitFileInfo(var FileInfo : TFileInfo; FileName,FilePath,FileType : string; FileSize : Double; FileDate : string);
begin
  FileInfo.FileName := FileName;
  FileInfo.FilePath := FilePath;
  FileInfo.FileType := FileType;
  FileInfo.FileSize := FileSize;
  FileInfo.FileDate := FileDate;
end;


function SortByName(FileInfo1,FileInfo2 : Pointer):Integer;
begin
  Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;

end;


function SortByPath(FileInfo1,FileInfo2 : Pointer):Integer;
begin

  Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

end;

function SortByType(FileInfo1,FileInfo2 : Pointer):Integer;
begin
  Result := ansicomparestr(PFileInfo(FileInfo2)^.FileType,PFileInfo(FileInfo1)^.FileType);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;

end;

function SortBySize(FileInfo1,FileInfo2 : Pointer):Integer;
var
  Diff,ComNum : double;
begin
  ComNum := 0.0000001;
  Diff := PFileInfo(FileInfo2)^.FileSize - PFileInfo(FileInfo1)^.FileSize;
  if Diff > 0 then
  begin
    Result := 1;
  end;
  if Diff < 0 then
  begin
    Result := -1;
  end;
  if (Diff < ComNum) and (Diff > ComNum*(-1)) then
  begin
     Result := 0;
  end;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;

end;

function SortByDate(FileInfo1,FileInfo2 : Pointer):Integer;
begin
  Result := ansicomparestr(PFileInfo(FileInfo2)^.FileDate,PFileInfo(FileInfo1)^.FileDate);

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FileName,PFileInfo(FileInfo1)^.FileName);
  end;

  if Result = 0 then
  begin
    Result := ansicomparestr(PFileInfo(FileInfo2)^.FilePath,PFileInfo(FileInfo1)^.FilePath);
  end;
end;

procedure Sorts(var List : TEMMemList; Num : Integer; IsSorted : Integer);
var
  Temp : PFileInfo;
  I,J : Integer;
  TempML : TEMMemList;
begin
  case Num of
    1 : List.Sort(SortByName);
    2 : List.Sort(SortByPath);
    3 : List.Sort(SortByType);
    4 : List.Sort(SortBySize);
    5 : List.Sort(SortByDate);
  end;

    //如果已经是降序排序，就反转List
  if(IsSorted = 1) then
  begin
    TempML := TEMMemList.Create(100);
    //J := (List.Count -1) div 2;
    for I := 0 to List.Count - 1 do
    begin
      TempML.Add(List[List.Count-1-I]);
    end;
    List := TempML;
    exit;
  end;

end;

end.
